#version 130

varying vec3 outNormal;
varying vec3 outTangent;
varying vec3 outColor;
varying vec2 outUV;
varying vec3 position;
varying vec3 outPosition;

uniform vec3 cameraPosition;
uniform vec3 LightPosition;

uniform vec3 AmbientalLightIntensity;

const vec3 diffuseColor=vec3(0.7,0.7,0.7);
const vec3 specularColor=vec3(0.5,0.5,0.5);

uniform sampler2D Texture;

uniform int with_texture;

void main() {
	vec3 color;
	if (with_texture == 1) {
		color= vec3(texture2D(Texture, outUV));
	} else {
		color = outColor;
	}
	//vec3 texcolor=vec3(texture2D(texture,outUV));
	vec3 normal = normalize(outNormal);
	vec3 surf2light = normalize(LightPosition-outPosition);
	vec3 surf2camera = normalize(cameraPosition-outPosition);
	vec3 reflection = -reflect(surf2camera,normal);
	
	float diffuseContribution = max(0.0,dot(normal,surf2light));
	float specularContribution = pow(max(0.0,dot(reflection,surf2light)),32.0);
	
	gl_FragColor = vec4(AmbientalLightIntensity*color+diffuseContribution *diffuseColor*color+ specularContribution*specularColor,1.0);

}
